package com.todod.service.impl;

import java.time.LocalDateTime;
import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.todod.common.util.SnowflakeIdGenerator;
import com.todod.mapper.RoleProjectMapper;
import com.todod.model.RoleProject;
import com.todod.service.IRoleProjectService;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author dyl
 * @date 2023-07-05
 */
@Service
@Transactional
public class RoleProjectServiceImpl extends ServiceImpl<RoleProjectMapper, RoleProject> implements IRoleProjectService {

	@Override
	public void deleteByRoleId(Long roleId) {
		QueryWrapper<RoleProject> wrapper = new QueryWrapper<RoleProject>();
		wrapper.eq("rp_role_id", roleId);

		remove(wrapper);
	}

	@Override
	public void deleteByProjectId(Long projectId) {
		QueryWrapper<RoleProject> wrapper = new QueryWrapper<RoleProject>();
		wrapper.eq("rp_project_id", projectId);

		remove(wrapper);
	}

	@Override
	public List<RoleProject> getRoleProject(Long roleId) {
		QueryWrapper<RoleProject> wrapper = new QueryWrapper<RoleProject>();
		wrapper.eq("rp_role_id", roleId);

		return list(wrapper);
	}

	@Override
	public void saveData(Long _userId, Long roleId, List<Long> projectIds) {
		deleteByRoleId(roleId);

		for (Long projectId : projectIds) {
			RoleProject roleProject = new RoleProject();
			roleProject.setId(SnowflakeIdGenerator.getId());
			roleProject.setCreateTime(LocalDateTime.now());
			roleProject.setCreateUserId(_userId);

			roleProject.setProjectId(projectId);
			roleProject.setRoleId(roleId);

			save(roleProject);
		}
	}

}
